/**
 * @author Ivan Holub
 */
class SongController {
    
    def show = {
        def songInstance = Song.get(params.id)
        def artistInstance = Artist.get(params.artistId)
        return [songInstance:songInstance, artistInstance:artistInstance]
    }

    def delete = {
        def songInstance = Song.get(params.id)
        def artistInstance = songInstance.artist
        songInstance.delete()
        flash.message = "Song ${params.id} deleted"
        redirect(controller:'artist', action:'show', id:artistInstance.id)
    }

    def edit = {
        def songInstance = Song.get(params.id)
        def artistInstance = songInstance.artist
        return [songInstance: songInstance, artistInstance: artistInstance]
    }

    def update = {
        def songInstance = Song.get(params.id)
        def artistInstance = songInstance.artist
        def isSuccess = false
        def songTitleOld = songInstance.title
        if (songTitleOld == params.title) {
            isSuccess = true
        }
        if (!isSuccess) {
            def songOther = artistInstance.songs.find {it.title == params.title};
            if (songOther == null) {
                isSuccess = true
            }
        }
        if (!isSuccess) {
            def songInstanceNew = new Song()
            songInstanceNew.properties = params
            songInstanceNew.id = songInstance.id
            songInstanceNew.errors.rejectValue("title", "", "Song with such title already exists.")
            render(view:'edit',model:[songInstance:songInstanceNew, artistInstance: artistInstance])
        }
        else {
            songInstance.properties = params
            if(!songInstance.hasErrors() && songInstance.save()) {
                flash.message = "Song ${params.id} updated"
                redirect(action:show,id:songInstance.id,params:[artistId:artistInstance.id])
            }
            else {
                render(view:'edit',model:[songInstance:songInstance, artistInstance: artistInstance])
            }
        }
    }

    def create = {
        def artistInstance = Artist.get(params.artistId)
        def songInstance = new Song()
        songInstance.properties = params
        return ['songInstance':songInstance, 'artistInstance':artistInstance]
    }

    def save = {
        def artistInstance = Artist.get(params.artistId)
        def isSuccess = false
        def songOther = artistInstance.songs.find {it.title == params.title};
        if (songOther == null) {
            isSuccess = true
        }
        def songInstance = new Song(params)
        if (!isSuccess) {
            songInstance.errors.rejectValue("title", "", "Song with such title already exists.")
        }
        songInstance.artist = artistInstance
        if(!songInstance.hasErrors() && songInstance.save()) {
            flash.message = "Song ${songInstance.id} created"
            redirect(action:show,params:[id:songInstance.id, artistId:artistInstance.id])
        }
        else {
            render(view:'create',model:[songInstance:songInstance, artistInstance:artistInstance])
        }
    }
}
